
/*-----------------------------------------------*
Name: Haqdarshak Pilot Intervention-Analysis (for Appendix)
Date Created: 30 July, 2018
Date Last Modified: 28 April, 2021
Created by: Aaron Berman and modified by Daniela Paz and Saumya Mathur on Nov.13/2020 
Uses Data: pilot_intervention_citizen_cumulative_all, pilot_intervention_tracking_sheets_all, and pilot_intervention_completed_apps
Creates Data: Latex Tables for Appendix
Description: Creates cross-price tables
*-------------------------------------------------*/



*..................................................
**Install user-written commands
foreach package in mdesc nmissing veracrypt {
     capture which `package'
	 if _rc==111 ssc install `package'
}
*..................................................
clear all
set more off
version 12.0
cap log close
pause off


//open log 
*cd "$logs"
*log using "analysis_cross_price_tables.smcl", replace


local crossprice = 1



//Looking at cross-price elasticies 
if `crossprice' == 1 {

	//obtain household screen treatment info	
	cd "$pilot"
	use "pilot_intervention_citizen_cumulative_all.dta", clear

	bysort household_id (screen_date): keep if _n == 1
	destring household_id, replace
	keep household_id discount
	rename discount hh_screen_discount

	tempfile hh_screen_treatment
	save `hh_screen_treatment', replace


	*------*
	//open full dataset with listing of tracking sheet observations
	cd "$pilot"
	use "pilot_intervention_tracking_sheets_all", clear 

	//merge in completed applications data 
	merge m:1 member_id scheme_id using "pilot_intervention_completed_apps"
	tab _merge
	drop if _merge == 2 //there should be 1 pension observation that doesn't merge 
	gen applied_scheme = (_merge == 2 | _merge == 3)
	drop _merge 

	//merge in household screening treatment status 
	merge m:1 household_id using `hh_screen_treatment'
	keep if _merge == 3
	drop _merge
	pause

	//save tempfile for later use
	tempfile schemes_all_main
	save `schemes_all_main', replace 
	pause


	*Stats for INDIVIDUALS 
	preserve
	bysort member_id: egen num_applied_schemes = total(applied_scheme)
	bysort member_id: egen num_agreed_schemes = total(agreed_scheme_amount)

	bysort member_id (scheme_offered_price): gen lowest_offered_price = scheme_offered_price[1]
	bysort member_id (scheme_offered_price): gen highest_offered_price = scheme_offered_price[_N]
	bysort member_id: gen num_offered_schemes = _N

	//dummies for if person agreed/applied to lowest-priced scheme (conditional on agreeing/applying)
	gen agreed_lowest_price = (agreed_scheme_amount == 1 & scheme_offered_price == lowest_offered_price) if num_agreed_schemes > 0
	gen applied_lowest_price = (applied_scheme == 1 & scheme_offered_price == lowest_offered_price) if num_applied_schemes > 0

	foreach x of numlist 0 25 50 100 200 {
		gen scheme_offered_`x' = (scheme_offered_price == `x')
		bysort member_id: egen num_scheme_offered_`x' = total(scheme_offered_`x')
	}

	bysort member_id (agreed_lowest_price): replace agreed_lowest_price = agreed_lowest_price[_N]
	bysort member_id (applied_lowest_price): replace applied_lowest_price = applied_lowest_price[_N]

	//make sure we have amount paid for scheme that was applied for 
	bysort member_id (amount): replace amount = amount[1] if num_applied_schemes > 0 

	pause
	
	*Tabs & matrices 
	//one observation per person 
	bysort member_id: keep if _n == 1

	//did individual apply for lowest price scheme 
	label define yesno 0 "No" 1 "Yes"
	label values agreed_lowest_price yesno 
	label values applied_lowest_price yesno 

	foreach x in agreed applied {
		eststo clear 
		estpost tab `x'_lowest_price 
		cd "$latex"
		#delimit ;
		esttab using "indiv_`x'_lowest_price.tex",
			label booktabs
			mlabels("Number of Individuals")
			varlabels(`e(labels)')
			varwidth(50)
			nonumbers 
			drop(Total) 
			nonotes
			nogaps
			fragment
			replace;
		#delimit cr

		eststo clear 
		estpost tab num_offered_schemes if num_`x'_schemes > 0
		cd "$latex"
		#delimit ;
		esttab using "indiv_`x'_number_of_schemes.tex",
			label booktabs
			mlabels("Number of Individuals")
			varlabels(`e(labels)')
			varwidth(50)
			nonumbers 
			drop(Total) 
			nonotes
			nogaps
			fragment
			replace;
		#delimit cr		

	}

	//matrix for determining frequency of prices offered to those who APPLIED for a scheme 
	matrix prices = J(6, 3, .)

	forv i = 1/3 {
		local x: word `i' of "0" "50" "100"
		forv j = 1/5 {
			local y: word `j' of "0" "25" "50" "100" "200"

			sum num_scheme_offered_`y' if num_applied_schemes > 0 & amount == `x'
			local num_offered_`y'_`x': di %8.2f `r(mean)'
			matrix prices[`j',`i'] = `num_offered_`y'_`x''
		}

		count if amount == `x'
		matrix prices[6, `i'] = `r(N)'
	}

	//output table
	cd "$latex"
	#delimit ;
	esttab matrix(prices) using "Price Frequencies_indiv_applied.tex",
		label booktabs
		collabels("Paid Rs. 0" "Paid Rs. 50" "Paid Rs. 100", lhs(Scheme Offer Price:))
		varlabels(r1 "Rs. 0" r2 "Rs. 25" r3 "Rs. 50" r4 "Rs. 100" r5 "Rs. 200" r6 "\midrule Number of Individuals")
		mlabels(none)
		replace;
	#delimit cr


	//Added 12/13: stats for people who did not take up lowest-price scheme 
	//number of schemes offered 
	eststo clear 
	estpost tab num_offered_schemes if applied_lowest_price == 0
	cd "$latex"
	#delimit ;
	esttab using "number_of_schemes_not_lowest_price.tex",
		label booktabs
		mlabels("Number of Individuals")
		varlabels(`e(labels)')
		varwidth(50)
		nonumbers 
		drop(Total) 
		nonotes
		nogaps
		fragment
		replace;
	#delimit cr		

	
	//Compare number of offered schemes b/w applied and not applied 
	pause
	label define yesno_applied 0 "Did Not Apply for Schemes" 1 "Applied for Scheme(s)"
	label values applied_scheme yesno_applied
	eststo clear 
	estpost tab num_offered_schemes applied_scheme
	cd "$latex"
	#delimit ;
	esttab using "scheme_offer_distribution.tex",
		label booktabs
		mlabels(none)
		varlabels(`e(labels)')
		varwidth(50)
		nonumbers 
		noobs
		nonotes
		nogaps
		unstack
		fragment
		replace;
	#delimit cr		
	pause

	restore

	//breakdown by individual 
	preserve
	bysort member_id: egen num_applied_schemes = total(applied_scheme)
	bysort member_id: egen num_agreed_schemes = total(agreed_scheme_amount)

	bysort member_id (scheme_offered_price): gen lowest_offered_price = scheme_offered_price[1]
	bysort member_id (scheme_offered_price): gen highest_offered_price = scheme_offered_price[_N]
	bysort member_id: gen num_offered_schemes = _N

	//dummies for if person agreed/applied to lowest-priced scheme (conditional on agreeing/applying)
	gen agreed_lowest_price = (agreed_scheme_amount == 1 & scheme_offered_price == lowest_offered_price) if num_agreed_schemes > 0
	gen applied_lowest_price = (applied_scheme == 1 & scheme_offered_price == lowest_offered_price) if num_applied_schemes > 0

	foreach x of numlist 0 25 50 100 200 {
		gen scheme_offered_`x' = (scheme_offered_price == `x')
		bysort member_id: egen num_scheme_offered_`x' = total(scheme_offered_`x')
	}

	bysort member_id (agreed_lowest_price): replace agreed_lowest_price = agreed_lowest_price[_N]
	bysort member_id (applied_lowest_price): replace applied_lowest_price = applied_lowest_price[_N]

	//make sure we have amount paid for scheme that was applied for 
	bysort member_id (amount): replace amount = amount[1] if num_applied_schemes > 0 

	//one observation per person 
	bysort member_id: keep if _n == 1

	keep if applied_lowest_price == 0
	sort amount 
	count
	local num_obs = `r(N)'
	matrix offers = J(`num_obs', 7, .)
	forv i = 1/`num_obs' {
		//offer price 
		local amount_`i' = amount[`i']
		matrix offers[`i', 1] = amount[`i'] 
		matrix offers[`i', 2] = num_scheme_offered_0[`i'] + num_scheme_offered_25[`i'] + num_scheme_offered_50[`i'] + num_scheme_offered_100[`i'] + num_scheme_offered_200[`i']

		forv j = 1/5 {
			local amount: word `j' of "0" "25" "50" "100" "200" 
			matrix offers[`i', `j'+2] = num_scheme_offered_`amount'[`i']
		}

	}

	//output table
	cd "$latex"
	#delimit ;
	esttab matrix(offers) using "Price Breakdown Not Lowest.tex",
		label booktabs
		collabels("Taken-Up Price" "Total Schemes Offered" "\# Rs. 0" "\# Rs. 25" "\# Rs. 50" "\# Rs. 100" "\# Rs. 200", lhs(Obs. Number:))
		varlabels(r1 "1" r2 "2" r3 "3" r4 "4" r5 "5" r6 "6")
		mlabels(none)
		replace;
	#delimit cr

	pause
	restore


	*Stats for HOUSEHOLDS
	preserve

	bysort household_id: egen num_applied_schemes = total(applied_scheme)
	bysort household_id: egen num_agreed_schemes = total(agreed_scheme_amount)

	bysort household_id (scheme_offered_price): gen lowest_offered_price = scheme_offered_price[1]
	bysort household_id (scheme_offered_price): gen highest_offered_price = scheme_offered_price[_N]
	bysort household_id: gen num_offered_schemes = _N

	//dummies for if HH agreed/applied to lowest-priced scheme (conditional on agreeing/applying)
	gen agreed_lowest_price = (agreed_scheme_amount == 1 & scheme_offered_price == lowest_offered_price) if num_agreed_schemes > 0
	gen applied_lowest_price = (applied_scheme == 1 & scheme_offered_price == lowest_offered_price) if num_applied_schemes > 0

	foreach x of numlist 0 25 50 100 200 {
		gen scheme_offered_`x' = (scheme_offered_price == `x')
		bysort household_id: egen num_scheme_offered_`x' = total(scheme_offered_`x')
	}

	bysort household_id (agreed_lowest_price): replace agreed_lowest_price = agreed_lowest_price[_N]
	bysort household_id (applied_lowest_price): replace applied_lowest_price = applied_lowest_price[_N]

	//make sure we have amount paid for scheme that was applied for 
	bysort household_id (amount): replace amount = amount[1] if num_applied_schemes > 0 

	//one observation per HH 
	bysort household_id: keep if _n == 1

	//did individual apply for lowest price scheme 
	label define yesno 0 "No" 1 "Yes"
	label values agreed_lowest_price yesno 
	label values applied_lowest_price yesno 

	foreach x in agreed applied {
		eststo clear 
		estpost tab `x'_lowest_price 
		cd "$latex"
		#delimit ;
		esttab using "hh_`x'_lowest_price.tex",
			label booktabs
			mlabels("Number of HHs")
			varlabels(`e(labels)')
			varwidth(50)
			nonumbers 
			drop(Total) 
			nonotes
			nogaps
			fragment
			replace;
		#delimit cr

		eststo clear 
		estpost tab num_offered_schemes if num_`x'_schemes > 0
		cd "$latex"
		#delimit ;
		esttab using "hh_`x'_number_of_schemes.tex",
			label booktabs
			mlabels("Number of HHs")
			varlabels(`e(labels)')
			varwidth(50)
			nonumbers 
			drop(Total) 
			nonotes
			nogaps
			fragment
			replace;
		#delimit cr		

	}

	//matrix for determining frequency of prices offered to those who APPLIED for a scheme 
	matrix prices = J(6, 3, .)

	forv i = 1/3 {
		local x: word `i' of "0" "50" "100"
		forv j = 1/5 {
			local y: word `j' of "0" "25" "50" "100" "200"

			sum num_scheme_offered_`y' if num_applied_schemes > 0 & amount == `x'
			local num_offered_`y'_`x': di %8.2f `r(mean)'
			matrix prices[`j',`i'] = `num_offered_`y'_`x''
		}

		count if amount == `x'
		matrix prices[6, `i'] = `r(N)'
	}

	//output table
	cd "$latex"
	#delimit ;
	esttab matrix(prices) using "Price Frequencies_HH_applied.tex",
		label booktabs
		collabels("Paid Rs. 0" "Paid Rs. 50" "Paid Rs. 100", lhs(Scheme Offer Price:))
		varlabels(r1 "Rs. 0" r2 "Rs. 25" r3 "Rs. 50" r4 "Rs. 100" r5 "Rs. 200" r6 "\midrule Number of Households")
		mlabels(none)
		replace;
	#delimit cr

	pause

	restore

}


